/*
*  Copyright 2019-2020  Sun Feng
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.ruibo.modules.quartz.service.impl;

import com.ruibo.modules.quartz.domain.QuartzLog;
import com.ruibo.utils.FileUtil;
import com.ruibo.utils.PageUtil;
import com.ruibo.utils.QueryHelp;
import com.ruibo.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import com.ruibo.modules.quartz.repository.QuartzLogRepository;
import com.ruibo.modules.quartz.service.QuartzLogService;
import com.ruibo.modules.quartz.service.dto.QuartzLogDto;
import com.ruibo.modules.quartz.service.dto.QuartzLogQueryCriteria;
import com.ruibo.modules.quartz.service.mapstruct.QuartzLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Sun Feng
* @date 2020-07-16
**/
@Service
@RequiredArgsConstructor
public class QuartzLogServiceImpl implements QuartzLogService {

    private final QuartzLogRepository quartzLogRepository;
    private final QuartzLogMapper quartzLogMapper;

    @Override
    public Map<String,Object> queryAll(QuartzLogQueryCriteria criteria, Pageable pageable){
        Page<QuartzLog> page = quartzLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(quartzLogMapper::toDto));
    }

    @Override
    public List<QuartzLogDto> queryAll(QuartzLogQueryCriteria criteria){
        return quartzLogMapper.toDto(quartzLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public QuartzLogDto findById(Long id) {
        QuartzLog quartzLog = quartzLogRepository.findById(id).orElseGet(QuartzLog::new);
        ValidationUtil.isNull(quartzLog.getId(),"QuartzLog","id",id);
        return quartzLogMapper.toDto(quartzLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public QuartzLogDto create(QuartzLog resources) {
        return quartzLogMapper.toDto(quartzLogRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(QuartzLog resources) {
        QuartzLog quartzLog = quartzLogRepository.findById(resources.getId()).orElseGet(QuartzLog::new);
        ValidationUtil.isNull( quartzLog.getId(),"QuartzLog","id",resources.getId());
        quartzLog.copy(resources);
        quartzLogRepository.save(quartzLog);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            quartzLogRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<QuartzLogDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (QuartzLogDto quartzLog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("任务名称", quartzLog.getJobName());
            map.put("实体名称", quartzLog.getBeanName());
            map.put("方法名称", quartzLog.getMethodName());
            map.put("参数", quartzLog.getParams());
            map.put("任务表达式", quartzLog.getCronExpression());
            map.put("是否成功", quartzLog.getIsSuccess());
            map.put("执行详情", quartzLog.getExceptionDetail());
            map.put("执行时间", quartzLog.getTime());
            map.put("创建时间", quartzLog.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
